﻿using System;
using System.Net;
using System.IO;
namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for MUAVN.
    /// </summary>
    public class BONGDA
    {
        #region Declare
        private SMS_MO WMO;
        private SMS_MT WMT;
        //ArrayList arr = new ArrayList();
        public BONGDA(SMS_MO MO)
        {
            WMO = MO;
        }

        public BONGDA()
        {
        }
        #endregion
        #region Method
        public void run()
        {
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            string[] msg = tmpMsg.Split(new char[] { ' ' });
            string strReturnMsg = GetReturnMsg(tmpMsg,msg);
            if (!string.IsNullOrEmpty(strReturnMsg))
            {
                WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                WMT.Insert();                                            
            }
            return;
        }
        private string GetReturnMsg(string tmpMsg,string[] arr)
        {
            string msgBody = String.Empty;
            if (WMO.CommandCode == "BC")
            {
                try
                {
                    if ((WMO.UserID == "84978925568") || (WMO.UserID == "84902222776") || (WMO.ServiceID.Replace("04", "") == "6672") || (WMO.ServiceID.Replace("04", "") == "6772"))
                    {
                        ServiceBongDa.BONGDA bongda = new ProcessesSMS.ServiceBongDa.BONGDA();
                        msgBody = bongda.UpdateCount(arr[0], arr[1], arr[2]);
                    }
                    else
                    {
                        msgBody = "Cu phap khong hop le.De du doan soan tin: BC X Y Z gui den 6672.Trong do X: Ma doi bong,Y la Thu tu xep hang (1,2,3),Z la so nguoi du doan giong ban";
                    }
                }
                catch
                {
                    msgBody = "Cu phap khong hop le.De du doan soan tin: BC X Y Z gui den 6672.Trong do X: Ma doi bong,Y la Thu tu xep hang (1,2,3),Z la so nguoi du doan giong ban";
                }
            }
            else if (WMO.CommandCode == "BD")
            {
                try
                {
                    string strurl = "";
                    if (tmpMsg == "MK")//lay lai mat khau
                    {
                        if (WMO.ServiceID.Replace("04", "") == "6072")
                        {
                            strurl = "De lay lai mat khau tren VBet soan tin: BD MK gui 6172.Luu y: Ban phai dung dung so dien thoai khi dang ky de nhan tin";
                        }
                        else
                        {
                            strurl = "http://vbet.vn/VITFBCMS/Service/lostpass.aspx?p=" + WMO.UserID + "&u=" + tmpMsg + "&smsin=" + WMO.Message + "&ds=" + WMO.ServiceID;
                        }
                    }
                    else if (tmpMsg == "HD")//nhan thong tin huong dan
                    {
                        if (WMO.ServiceID.Replace("04", "") == "6072")
                        {
                            strurl = "De nhan thong tin huong dan chi tiet tu VBet soan tin: BD HD gui 6172. Chuc ban may man!";
                        }
                        else
                        {
                            strurl = "http://vbet.vn/VITFBCMS/Service/huongdan.aspx?p=" + WMO.UserID + "&u=" + tmpMsg + "&smsin=" + WMO.Message + "&ds=" + WMO.ServiceID;
                        }
                    }
                    else//nap tien tai khoan + kich hoat tai khoan
                    {
                        if (WMO.ServiceID.Replace("04", "") == "6072")
                        {
                            msgBody = "De nap tien vao tai khoan soan: BD TenTaiKhoan gui 6672 hoac 6772.Neu tai khoan chua kich hoat soan tin: BD TenTaiKhoan gui 6172 de kich hoat";
                        }
                        else if ((WMO.ServiceID.Replace("04", "") == "6672") || (WMO.ServiceID.Replace("04", "") == "6772"))
                        {
                            strurl = "http://vbet.vn/VITFBCMS/Service/VitSms.aspx?p=" + WMO.UserID + "&u=" + tmpMsg + "&smsin=" + WMO.Message + "&ds=" + WMO.ServiceID;
                        }
                        else
                        {
                            strurl = "http://vbet.vn/VITFBCMS/Service/KichHoat.aspx?p=" + WMO.UserID + "&u=" + tmpMsg + "&smsin=" + WMO.Message + "&ds=" + WMO.ServiceID;
                        }
                    }
                    msgBody = GetMsgStockFromURL(strurl);
                    if (msgBody.ToUpper() == "ERR")
                        msgBody = "";
                }
                catch
                {
                    msgBody = "";
                }
            }
            return msgBody;
        }
        //GetDataFromURL
        private string GetMsgStockFromURL(string url)
        {
            string strval = "";
            WebRequest WQob = WebRequest.Create(url);
            WebResponse WPob = WQob.GetResponse();
            StreamReader SRob = new StreamReader(WPob.GetResponseStream());
            strval = SRob.ReadToEnd();
            WQob.Abort();
            return strval;
        }
        #endregion
    }
}
